package client.comm.baoding.ui

import android.content.Intent
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import androidx.lifecycle.Observer
import client.comm.commlib.base.FinishReciver
import client.comm.commlib.toast.toast
import client.comm.commlib.utils.SharedUtil
import client.comm.baoding.R
import client.comm.baoding.databinding.ActivityXgmmBinding
import client.comm.baoding.ui.vm.XgmmViewModel
import client.comm.baoding.BaseMiddleActivity

class XgmmActivity : BaseMiddleActivity<XgmmViewModel, ActivityXgmmBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_xgmm
    }

    override fun initView() {
        binding?.apply {
            event = this@XgmmActivity
            vm = viewModel
        }
    }

    override fun observerUI() {
        viewModel.forgetRet.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("找回成功")
                    SharedUtil.clear("token")
                    startActivity(Intent(this@XgmmActivity, LoginActivity::class.java))
                    sendBroadcast(Intent(FinishReciver.FINISH_ACTION))
                } else {
                    toast(it.message)
                }
            }
        })

        viewModel.isShowPwdOld.observe(this, Observer {
            if (it) {
                binding.etPwdOld.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                binding. etPwdOld.setSelection((binding.etPwdOld.getText().toString().trim()).length);
            } else {
                binding. etPwdOld.setTransformationMethod(PasswordTransformationMethod.getInstance());
                binding. etPwdOld.setSelection((binding.etPwdOld.getText().toString().trim()).length);
            }
        })
        viewModel.isShowPwd.observe(this, Observer {
            if (it) {
                binding.etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                binding.etPwd.setSelection((binding.etPwd.getText().toString().trim()).length);
            } else {
                binding.etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                binding. etPwd.setSelection((binding.etPwd.getText().toString().trim()).length);
            }
        })
        viewModel.isShowPwdAgain.observe(this, Observer {
            if (it) {
                binding.etPwdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                binding.etPwdAgain.setSelection((binding.etPwdAgain.getText().toString().trim()).length);
            } else {
                binding.etPwdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
                binding. etPwdAgain.setSelection((binding.etPwdAgain.getText().toString().trim()).length);
            }
        })

    }

    override fun initViewModel(): XgmmViewModel = createViewModel()

}